package com.xyebank.ledger.service.xyd.adaper;

import com.xyebank.ledger.db.entitys.LApplicationInf;
import com.xyebank.ledger.service.common.conveter.ApplyBeanConverter;
import com.xyebank.ledger.service.constant.CommonConstant;
import com.xyebank.ledger.service.constant.ProductConstant;
import com.xyebank.ledger.util.DateConverter;
import com.xyebank.ledger.util.DateUtil;
import com.xyebank.ledger.util.IDUtil;
import com.xyebank.ledger.vo.deal.XYDCashApplyVo;

import org.modelmapper.AbstractConverter;
import org.modelmapper.Converter;
import org.modelmapper.ModelMapper;
import org.modelmapper.PropertyMap;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component(value = "xydApplyBeanConverter")
public class XYDApplyBeanConverter implements ApplyBeanConverter<XYDCashApplyVo> {

    ModelMapper modelMapper = new ModelMapper();

	// 不能采取注入方式
	private DateConverter dateConverter = new DateConverter();

    class NXApplyPropertyMap extends PropertyMap<XYDCashApplyVo, LApplicationInf> {
        // 注意 不要有重复的映射，不然会报错
        protected void configure() {
            // 订单号
            map(source.getOutorderNumber(), destination.getXyOrderNo());
            // 电话号码
            map(source.getPrimaryPhone(), destination.getMblNo());
            // 申请时间
            map(source.getCreTime(), destination.getApplyTime());
            // 创建时间
            map(source.getCreTime(), destination.getCreditTime());
            // 状态
            // map(source.getStatus(), destination.getStatus());

        }
    }

    public XYDApplyBeanConverter() {
        modelMapper.addMappings(new NXApplyPropertyMap());
    }

    @Override
    public LApplicationInf covert(XYDCashApplyVo x) {
        LApplicationInf entity = modelMapper.map(x, LApplicationInf.class);
        entity.setDataSource(x.getOutorderNumber().length() == 16 ? CommonConstant.JRY_STR : CommonConstant.SJD_STR);
        entity.setXyOrgNo(ProductConstant.XYD_LOAN_ORG);
        entity.setXyProductNo(ProductConstant.XYD_LOAN_PROD_ID);
        entity.setId(IDUtil.generateId(x.getId().toString(), "xyd_apply"));
        String status = x.getStatus();
        if ("success".equals(status) || "AUDIT_SUCCESS".equals(status)) {
            entity.setStatus(CommonConstant.APPLY_SUCCESS);
        } else {
            entity.setStatus(CommonConstant.APPLY_FAIL);
        }

        return entity;
    }
}
